# -*- coding: utf-8 -*-
# copyright (c) 2007
# Kazumasa Suzuki
from axiom.item import Item
from axiom.attributes import text, timestamp, reference, AND, OR
from axiom.store import Store
#date related
from epsilon import extime
import datetime
import time
#import operator

class JST(datetime.tzinfo):
    def tzname(self, dt):
        return 'JST'
    def utcoffset(self, dt):
        return datetime.timedelta(hours = +9)
    def dst(self, dt):
        return datetime.timedelta(0)

class MtMessage(Item):
    typeName = 'message'
    schemaVersion = 1
    who = text()
    subject = text()
    date = timestamp()
    body = text()
    room = reference()
    roomname = text()

class MtRoom(Item):
    typeName = 'room'
    schemaVersion = 1
    name = text()
    owner = reference() 
    roomObject = reference()


class MtUser(Item):
    typeName = 'User'
    schemaVersion = 1
    username = text() # ユーザ識別情報
    nickname = text() # 表示名
    myroom = text() # とりあえずroom は1つしか入れない。
    # あとで複数にはいれるようにしないといけない。
    
class MidtownManager(object):
    def __init__(self):
        self.store = Store('midtown.axiom', debug = False)
       #store = Store()

    def getAllMessages(self):
        return self.store.query(MtMessage)

    def getAllRoomMessages(self, roomname=u'test'):
        return self.store.query(MtMessage, MtMessage.roomname == roomname)
        #room = self.store.query(MtRoom, MtRoom.name == roomname)
        #return room[0].msglist

    def getLogins(self, roomname=u'test'):
        #for u in self.store.query(User):
        #    print 'no users found'
        return self.store.query(User, User.myroom==roomname)

__all__ = ["JST","MtRoom","MtUser","MtMessage","MidtownManager"
           ]

if __name__ == "__main__":
    # for testing
    mm = MidtownManager()
    for msg in mm.getAllMessages():
        print msg.roomname
        print msg.who
        print msg.subject
        print ''.join(msg.body)

    '''

    aRoom = MtRoom(name = u'test', store = mm.store)
    query_result = mm.store.query(MtRoom, MtRoom.name == u'test')
    for room in query_result:
        print room.name
        if room.name == u'test':
            print 'MtRoom test pass'
    
    aUser = MtUser(username = u'guest', 
                   nickname = u'MidTownGuest',
                   myroom = u'test',
                   store = mm.store)
    aRoom.owner = aUser
    
    aMessage = MtMessage(who = aUser.nickname,
                         subject = u'MidTownTest',
                         body = u'Blah Blah Blah',
                         room = aRoom,
                         roomname = aRoom.name,
                         store = mm.store)

    bMessage = MtMessage(who = aUser.nickname,
                         subject = u'MidTownTest2',
                         body = u'Blah Blah Blah, Foo Bar.',
                         room = aRoom,
                         roomname = aRoom.name,
                         store = mm.store)

    sub_query_result = mm.store.query(MtMessage, 
                                      MtMessage.subject == u'MidTownTest')
    for sub in sub_query_result:
        print 'subject:', sub.body
        print 'roomname:', sub.room.name
        print 'room owner: ', sub.room.owner.nickname

    roommsg_query_result = aRoom.getAllMessages()
    print roommsg_query_result.count()
    for msg in roommsg_query_result:
        print msg.subject
'''
    
